﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using Assist;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit.RevitExtension;
using YJKArch.Utils;
using YJKControls.Previewer;

namespace YJKArch.SlopeDim
{
	public partial class SlopeDim : System.Windows.Forms.Form
	{
		public bool InfoChanged
		{
			get
			{
				return this.infoChanged;
			}
			set
			{
				this.infoChanged = value;
			}
		}

		public bool IsContinueCmd { get; set; }

		private bool IsCanSendMsg { get; set; }

		public SlopeDim(ExternalCommandData ecd)
		{
			this.pictureBox1 = new Previewer(1u);
			this.pictureBox1.Paint += this.pictureBox1_Paint;
			base.Controls.Add(this.pictureBox1);
			this.pictureBox1.Location = new System.Drawing.Point(198, 11);
			this.pictureBox1.Size = new Size(129, 141);
			this.pictureBox1.TabIndex = 4;
			this.pictureBox1.TabStop = false;
			this.IsContinueCmd = false;
			this.IsCanSendMsg = false;
			this.InitializeComponent();
			this.revit = ecd;
			this.RefreshType();
			if (this.cbxArrow.Items.Count > 0)
			{
				this.cbxArrow.SelectedIndex = 0;
			}
		}

		private void SlopeDim_Load(object sender, EventArgs e)
		{
			this.ReadSlopDimData();
			this.RefreshPreview();
			base.Icon = Resource.YArchitechIcon;
			this.IsContinueCmd = false;
			this.IsCanSendMsg = true;
		}

		private void pictureBox1_Paint(object sender, PaintEventArgs e)
		{
			Graphics graphics = e.Graphics;
			graphics.Clear(System.Drawing.Color.White);
			this.RefreshPreview(graphics);
		}

		private void RefreshType()
		{
			this.cbxStyle.Items.Clear();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.revit.Application.ActiveUIDocument.Document);
			filteredElementCollector.OfClass(typeof(TextNoteType));
			if (filteredElementCollector != null)
			{
				IEnumerable<Element> enumerable = filteredElementCollector.ToElements();
				new List<Element>();
				foreach (Element element in enumerable)
				{
					this.cbxStyle.Items.Add(new ShowType(element as TextNoteType));
				}
			}
			if (this.cbxStyle.Items.Count > 0)
			{
				this.cbxStyle.SelectedIndex = 0;
			}
		}

		private void RefreshPreview(Graphics g)
		{
			if (this.InfoChanged)
			{
				this.InfoChanged = false;
				List<TextNoteInfo> list = new List<TextNoteInfo>();
				List<CurveInfo> list2 = new List<CurveInfo>();
				List<FillRegion> list3 = new List<FillRegion>();
				try
				{
					double num = Convert.ToDouble(this.txtAngle.Text) / 180.0 * Math.PI;
					double num2 = 60.0;
					double num3 = -20.0;
					Line line = YJKLineEx.YJKGetBound(new XYZ(-num2 / 2.0, num3, 0.0), new XYZ(num2 / 2.0, num3, 0.0));
					XYZ xyz = line.GetEndPoint(0);
					XYZ xyz2 = line.GetEndPoint(1);
					XYZ xyz3 = XYZ.Zero;
					XYZ xyz4 = XYZ.Zero;
					if (this.cbxArrow.SelectedItem.ToString() == "实心箭头" || this.cbxArrow.SelectedItem.ToString() == "空心箭头")
					{
						xyz3 = new XYZ(xyz.X + 5.0, xyz.Y + 2.0, 0.0);
						xyz4 = new XYZ(xyz.X + 5.0, xyz.Y - 2.0, 0.0);
					}
					else if ((Convert.ToDouble(this.txtAngle.Text) >= 90.0 && Convert.ToDouble(this.txtAngle.Text) <= 180.0) || (Convert.ToDouble(this.txtAngle.Text) >= 180.0 && Convert.ToDouble(this.txtAngle.Text) < 270.0))
					{
						xyz3 = new XYZ(xyz.X + 5.0, xyz.Y - 2.0, 0.0);
						xyz4 = new XYZ(xyz.X + 5.0, xyz.Y, 0.0);
					}
					else
					{
						xyz3 = new XYZ(xyz.X + 5.0, xyz.Y + 2.0, 0.0);
						xyz4 = new XYZ(xyz.X + 5.0, xyz.Y, 0.0);
					}
					Line line2 = YJKLineEx.YJKGetBound(xyz, xyz3);
					Line line3 = YJKLineEx.YJKGetBound(xyz3, xyz4);
					Line line4 = YJKLineEx.YJKGetBound(xyz4, xyz);
					XYZ xyz5 = LineAssistFunc.Midpoint(line);
					xyz = Geometry.RotateTo(xyz, xyz5, XYZ.BasisZ, -num);
					xyz2 = Geometry.RotateTo(xyz2, xyz5, XYZ.BasisZ, -num);
					line = YJKLineEx.YJKGetBound(xyz, xyz2);
					line2 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line2.GetEndPoint(0), xyz5, XYZ.BasisZ, -num), Geometry.RotateTo(line2.GetEndPoint(1), xyz5, XYZ.BasisZ, -num));
					line3 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line3.GetEndPoint(0), xyz5, XYZ.BasisZ, -num), Geometry.RotateTo(line3.GetEndPoint(1), xyz5, XYZ.BasisZ, -num));
					line4 = YJKLineEx.YJKGetBound(Geometry.RotateTo(line4.GetEndPoint(0), xyz5, XYZ.BasisZ, -num), Geometry.RotateTo(line4.GetEndPoint(1), xyz5, XYZ.BasisZ, -num));
					CurveInfo item = new CurveInfo(line, PenColor.PC_Green, 1f);
					CurveInfo item2 = new CurveInfo(line2, PenColor.PC_Green, 1f);
					CurveInfo item3 = new CurveInfo(line3, PenColor.PC_Green, 1f);
					CurveInfo item4 = new CurveInfo(line4, PenColor.PC_Green, 1f);
					list2.Add(item);
					list2.Add(item2);
					list2.Add(item3);
					list2.Add(item4);
					if (this.cbxArrow.SelectedItem.ToString() == "实心箭头" || this.cbxArrow.SelectedItem.ToString() == "实心半箭头")
					{
						FillRegion item5 = new FillRegion(new List<Curve>
						{
							line2,
							line3,
							line4
						}, PenColor.PC_Green);
						list3.Add(item5);
					}
					xyz5 = LineAssistFunc.Midpoint(line);
					XYZ xyz6 = (line.GetEndPoint(1) - xyz5).Normalize() * 10.0;
					double boundingBoxWidth = (double)(this.cbxValue.Text.Length * 10);
					if ((Convert.ToDouble(this.txtAngle.Text) >= 90.0 && Convert.ToDouble(this.txtAngle.Text) <= 180.0) || (Convert.ToDouble(this.txtAngle.Text) >= 180.0 && Convert.ToDouble(this.txtAngle.Text) < 270.0))
					{
						xyz6 = Geometry.RotateTo(xyz6, -Math.PI*.5, XYZ.BasisZ);
						TextNoteInfo item6 = new TextNoteInfo(this.cbxValue.Text, PenColor.PC_Green, 10.0, xyz5 + xyz6, boundingBoxWidth, 15.0, -num + Math.PI);
						list.Add(item6);
					}
					else
					{
						xyz6 = Geometry.RotateTo(xyz6, Math.PI*.5, XYZ.BasisZ);
						TextNoteInfo item7 = new TextNoteInfo(this.cbxValue.Text, PenColor.PC_Green, 10.0, xyz5 + xyz6, boundingBoxWidth, 15.0, -num);
						list.Add(item7);
					}
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.Message, "盈建科提示");
				}
				this.pictureBox1.TextNotes = list;
				this.pictureBox1.Curves = list2;
				this.pictureBox1.FillRegions = list3;
				this.pictureBox1.ZoomExtents();
			}
		}

		private void cbxValue_KeyPress(object sender, KeyPressEventArgs e)
		{
			string input = e.KeyChar.ToString();
			if (!new Regex("^(\\d|\\.|:|：|%){1}$").IsMatch(input))
			{
				e.Handled = true;
			}
		}

		private void txtAngle_KeyPress(object sender, KeyPressEventArgs e)
		{
			if (!char.IsNumber(e.KeyChar) && e.KeyChar != '\r' && e.KeyChar != '\b')
			{
				e.Handled = true;
			}
		}

		private void txtAngle_TextChanged(object sender, EventArgs e)
		{
			if (this.txtAngle.Text == "")
			{
				this.txtAngle.Text = "0";
			}
			if (this.angle != this.txtAngle.Text)
			{
				this.angle = this.txtAngle.Text;
				this.RefreshPreview();
			}
		}

		private void cbxArrow_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.arrow != this.cbxArrow.SelectedIndex)
			{
				this.arrow = this.cbxArrow.SelectedIndex;
				this.RefreshPreview();
				if (this.IsCanSendMsg)
				{
					int wMsg = 7;
					SlopeDim.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
					this.IsContinueCmd = true;
					this.SendESC();
				}
			}
		}

		private void cbxValue_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.value != this.cbxValue.SelectedItem.ToString())
			{
				this.value = this.cbxValue.Text;
				this.RefreshPreview();
				if (this.IsCanSendMsg)
				{
					int wMsg = 7;
					SlopeDim.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
					this.IsContinueCmd = true;
					this.SendESC();
				}
			}
		}

		private void cbxValue_TextChanged(object sender, EventArgs e)
		{
			if (this.value != this.cbxValue.Text)
			{
				this.value = this.cbxValue.Text;
				this.RefreshPreview();
			}
		}

		private void cbxStyle_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (this.style != this.cbxStyle.SelectedIndex)
			{
				this.style = this.cbxStyle.SelectedIndex;
				this.RefreshPreview();
				if (this.IsCanSendMsg)
				{
					int wMsg = 7;
					SlopeDim.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
					this.IsContinueCmd = true;
					this.SendESC();
				}
			}
		}

		private void RefreshPreview()
		{
			this.InfoChanged = true;
			this.pictureBox1.Refresh();
		}

		[DllImport("user32.dll")]
		public static extern void keybd_event(byte bVk, byte bScan, int dwFlags, int dwExtraInfo);

		private void SlopeDim_FormClosed(object sender, FormClosedEventArgs e)
		{
			this.IsContinueCmd = false;
			this.SaveSlopDimData();
			this.SendESC();
		}

		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		private void SlopeDim_MouseLeave(object sender, EventArgs e)
		{
			if (!base.ClientRectangle.Contains(base.PointToClient(Cursor.Position)))
			{
				int wMsg = 7;
				SlopeDim.SendMessage(WindowHelper.HWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				if (CmdSlopeDim.v.value != this.cbxValue.Text || CmdSlopeDim.v.angle != this.txtAngle.Text)
				{
					this.IsContinueCmd = true;
					this.SendESC();
					return;
				}
			}
			else
			{
				base.Focus();
			}
		}

		private void SendESC()
		{
			int bVk = 27;
			int dwFlags = 0;
			int dwFlags2 = 2;
			SlopeDim.keybd_event((byte)bVk, 0, dwFlags, 0);
			SlopeDim.keybd_event((byte)bVk, 0, dwFlags2, 0);
		}

		private void ReadSlopDimData()
		{
			string path = Path.GetTempPath() + "SlopDim.dat";
			if (!File.Exists(path))
			{
				return;
			}
			FileStream fileStream = new FileStream(path, FileMode.Open);
			StreamReader streamReader = new StreamReader(fileStream);
			streamReader.BaseStream.Seek(0L, SeekOrigin.Begin);
			string text = streamReader.ReadLine();
			if (!string.IsNullOrEmpty(text))
			{
				this.cbxValue.Text = text;
			}
			text = streamReader.ReadLine();
			if (!string.IsNullOrEmpty(text))
			{
				this.txtAngle.Text = text;
			}
			text = streamReader.ReadLine();
			if (!string.IsNullOrEmpty(text))
			{
				this.cbxArrow.SelectedIndex = Convert.ToInt32(text);
			}
			text = streamReader.ReadLine();
			if (!string.IsNullOrEmpty(text))
			{
				this.cbxStyle.SelectedIndex = Convert.ToInt32(text);
			}
			streamReader.Close();
			fileStream.Close();
		}

		private void SaveSlopDimData()
		{
			FileStream fileStream = new FileStream(Path.GetTempPath() + "SlopDim.dat", FileMode.Create);
			StreamWriter streamWriter = new StreamWriter(fileStream);
			streamWriter.WriteLine(this.cbxValue.Text);
			streamWriter.WriteLine(this.txtAngle.Text);
			streamWriter.WriteLine(this.cbxArrow.SelectedIndex.ToString());
			streamWriter.WriteLine(this.cbxStyle.SelectedIndex.ToString());
			streamWriter.Flush();
			streamWriter.Close();
			fileStream.Close();
		}

		private string value = "2%";

		private string angle = "0";

		private int arrow;

		private int style;

		private ExternalCommandData revit;

		private Previewer pictureBox1;

		private bool infoChanged;
	}
}
